home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / PowerPC / Dev / PPCRelease / Examples / IPC / Msg3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-07  |  5.8 KB  |  194 lines

  1. /* Msg3 and Msg3PPC.elf
  2.  *
  3.  * This test program sends 1000 messages to the PPC and
  4.  * shows how long this takes.
  5.  * The Messages are send synchron and every msg must
  6.  * be replied after another.
  7.  * Each Message has the Body "Text sent by M68k processor\n"
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/nodes.h>
  12. #include <exec/lists.h>
  13. #include <exec/memory.h>
  14. #include <utility/tagitem.h>
  15. #include <powerup/ppclib/interface.h>
  16. #include <powerup/ppclib/message.h>
  17. #include <powerup/ppclib/tasks.h>
  18. #include <powerup/proto/ppc.h>
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "time.h"
  24. #include "time_protos.h"
  25.  
  26. #define TEXT            "Text sent by M68k processor\n"
  27. #define    MSGCOUNT    1000
  28.  
  29. struct StartupData
  30. {
  31.     ULONG    MsgCount;
  32. };
  33.  
  34. extern struct Library    *SysBase;
  35.  
  36. int    main(void)
  37. {
  38. struct Library        *PPCLibBase;
  39. struct TagItem        MyTags[10];
  40. void            *PPCPort;
  41. void            *ReplyPort;
  42. void            *StartupMsg;
  43. void            *M68kMsg;
  44. void            *PPCMsg;
  45. void            *ElfObject;
  46. void            *Task;
  47. UBYTE            *Body;
  48. struct StartupData    *StartupData;
  49. void            *MyTimerObject;
  50. ULONG            i;
  51.  
  52.   Printf("Opening ppc.library\n");
  53.   if (PPCLibBase = OpenLibrary("ppc.library", 44))
  54.   {
  55.     if (MyTimerObject=TimerCreateObject())
  56.     {
  57.       Printf("Loading PPC object\n");
  58.       if (ElfObject=PPCLoadObject("PROGDIR:Msg3PPC.elf"))
  59.       {
  60.         Printf("Creating reply port\n");
  61.         MyTags[0].ti_Tag    =    TAG_DONE;
  62.         if (ReplyPort = PPCCreatePort(MyTags))
  63.         {
  64.           if (StartupMsg = PPCCreateMessage(ReplyPort, 0))
  65.           {
  66.             Printf("Allocating StartupData\n");
  67.             if (StartupData = PPCAllocVec(sizeof(struct StartupData), MEMF_ANY))
  68.             {
  69.               StartupData->MsgCount    =    MSGCOUNT;
  70.  
  71.               Printf("Creating PPC task\n");
  72.               MyTags[0].ti_Tag    =    PPCTASKTAG_STARTUP_MSG;
  73.               MyTags[0].ti_Data    =(ULONG) StartupMsg;
  74.  
  75.               MyTags[1].ti_Tag    =    PPCTASKTAG_STARTUP_MSGDATA;
  76.               MyTags[1].ti_Data    =(ULONG) StartupData;
  77.  
  78.               MyTags[2].ti_Tag    =    PPCTASKTAG_STARTUP_MSGLENGTH;
  79.               MyTags[2].ti_Data    =    0;
  80.  
  81.               MyTags[3].ti_Tag    =    PPCTASKTAG_STARTUP_MSGID;
  82.               MyTags[3].ti_Data    =    0;
  83.  
  84.               MyTags[4].ti_Tag    =    PPCTASKTAG_MSGPORT;
  85.               MyTags[4].ti_Data    =    TRUE;
  86.  
  87.               MyTags[5].ti_Tag    =    TAG_DONE;
  88.  
  89.               if (Task = PPCCreateTask(ElfObject, MyTags))
  90.               {
  91.                 if (PPCPort=(void*) PPCGetTaskAttrsTags(Task,
  92.                                                         PPCTASKINFOTAG_MSGPORT,0,
  93.                                                         TAG_END))
  94.                 {
  95.                   Printf("Allocating memory for message body\n");
  96.                   if (Body = PPCAllocVec(sizeof(TEXT), MEMF_PUBLIC))
  97.                   {
  98.                     Printf("Creating message...\n");
  99.                     if (PPCMsg = PPCCreateMessage(ReplyPort, sizeof(TEXT)))
  100.                     {
  101.                       strcpy(Body, TEXT);
  102.                       Printf("Sending 1000 SYNCHRON messages with a *Body* and wait for each reply...");
  103.  
  104.                       TimerSetAttr(MyTimerObject,TIMERTAG_START);
  105.                       for (i=0;i<MSGCOUNT;i++)
  106.                       {
  107.                         PPCSendMessage(PPCPort,
  108.                                        PPCMsg,
  109.                                        Body,
  110.                                        sizeof(TEXT),
  111.                                        0x12345678);
  112.                         PPCWaitPort(ReplyPort);
  113.                         PPCGetMessage(ReplyPort);
  114.                       }
  115.                       TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  116.                       TimerShow(MyTimerObject);
  117.  
  118.                       Printf("Waiting for Task Finish Msg...\n");
  119.                       for (;;)
  120.                       {
  121.                         if ((M68kMsg=PPCGetMessage(ReplyPort)) == StartupMsg)
  122.                         {
  123.                           Printf("Ahh..the expected Startup=Finish Msg was received.\nNow we can savely free all resources.\n");
  124.                           break;
  125.                         }
  126.                         else
  127.                         {
  128.                           Printf("Some non replied Msg 0x%lx was found..wait\n",
  129.                                  M68kMsg);
  130.                           PPCWaitPort(ReplyPort);
  131.                         }
  132.                       }
  133.  
  134.                       Printf("Deleting message...\n");
  135.                       PPCDeleteMessage(PPCMsg);
  136.                     }
  137.                     else
  138.                     {
  139.                       Printf("Could not create Msg\n");
  140.                     }
  141.                     PPCFreeVec(Body);
  142.                   }
  143.                   else
  144.                   {
  145.                     Printf("Could not alloc mem for msg body\n");
  146.                   }
  147.                 }
  148.                 else
  149.                 {
  150.                   Printf("Could not find the PPCTask's msgport\n");
  151.                 }
  152.               }
  153.               else
  154.               {
  155.                 Printf("Could not create PPC task\n");
  156.               }
  157.               PPCFreeVec(StartupData);
  158.             }
  159.             else
  160.             {
  161.               Printf("Could not alloc Startup Data\n");
  162.             }
  163.             PPCDeleteMessage(StartupMsg);
  164.           }
  165.           else
  166.           {
  167.             Printf("Could not create Startup message\n");
  168.           }
  169.           PPCDeletePort(ReplyPort);
  170.         }
  171.         else
  172.         {
  173.           Printf("Could not create reply port\n");
  174.         }
  175.         Printf("Unloading PPC object\n");
  176.         PPCUnLoadObject(ElfObject);
  177.       }
  178.       else
  179.       {
  180.         Printf("Could not load the elfobject\n");
  181.       }
  182.       TimerDeleteObject(MyTimerObject);
  183.     }
  184.     Printf("Closing ppc.library\n");
  185.     CloseLibrary(PPCLibBase);
  186.   }
  187.   else
  188.   {
  189.     Printf("Could not open ppc.library v44+\n");
  190.   }
  191.   return(0);
  192. }
  193.  
  194.